/*
 *   This is a NT Service, which pings a number of sites in a random order
 *   to keep your internet connection alive.
 *   
 *   Copyright (C) 2004  Jess Nielsen (jnielsen@freesiteuk.co.uk)
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if !defined(AFX_FILELOGGING_H__6713B666_22BD_493C_A88B_993124D09BB3__INCLUDED_)
#define AFX_FILELOGGING_H__6713B666_22BD_493C_A88B_993124D09BB3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "comutil.h"
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>
#include "winbase.h"
#include <iostream>

extern const char FILENAME[MAX_PATH];

class CFileLogging  
{
public:
	static CFileLogging * get_FileLogging() {
		if (_self == NULL)
			_self = new CFileLogging;

		return _self;
	}
	static void LogToFile(const char * message, bool releaseLog = FALSE) {
		CFileLogging * _this = CFileLogging::get_FileLogging();
		_this->WriteToFile(message, releaseLog);
	}

	void WriteToFile(const char * message, bool releaseLog = FALSE) {

#ifdef _RELEASE
		if (!releaseLog || 0 >= gLogAll)
			return;
#endif

		FILE * fp = fopen(FILENAME, "a+");
		if (NULL != fp) {

			TCHAR subj[MAX_PATH*2] = "";
			time_t timePoint;
			struct _timeb timebuffer;
			char *timeline;
			char *temp = (char*)message;

			_ftime( &timebuffer );
			timePoint = timebuffer.time;
			timeline = ctime( & ( timebuffer.time ) );
			sprintf(subj, "%.19s.%hu :: ", timeline, timebuffer.millitm);
			strcat(subj, temp);
			strcat(subj, "\n");

			fwrite(subj, 1, strlen(subj), fp);
			fclose(fp);
		}
	}
protected:
	CFileLogging();
	virtual ~CFileLogging();
private:
	static CFileLogging * _self;	
};

#endif // !defined(AFX_FILELOGGING_H__6713B666_22BD_493C_A88B_993124D09BB3__INCLUDED_)
